import {mapGetters} from "vuex";

export const mixins = {
  data() {
    return {
      loading: true,
      form: {},
      query: {
        current:1,
        size:10,
      },
      selectionList: [],
      dataList: [],
      //permissionList:[],
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      option:{},
    }
  },
  computed: {
    ...mapGetters(["userInfo", "permission"]),
     permissionList() {
      return {
        // addBtn: this.vaildData(this.permission.xxx_add,false),
        // viewBtn: this.vaildData(this.permission.xxx_view,false),
        // delBtn: this.vaildData(this.permission.xxx_delete,false),
        // editBtn: this.vaildData(this.permission.xxx_edit,false)
      };
     },

    ids() {
      const ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele[this.rowKey]);
      });
      return ids.join(",");
    },

    onEvent() {
      return {
        'on-load': this.getList,
        'row-save': this.rowSave,
        'row-update': this.rowUpdate,
        'row-del': this.rowDel,
        'selection-change': this.selectionChange,
        'refresh-change': this.refreshChange,
        'date-change': this.dateChange,
        'search-change': this.searchChange,
        'search-reset': this.searchChange
      }
    },

    rowKey() {
      return this.option.rowKey || 'id'
    }
  },


  methods: {

    onLoad(page, params = {}) {
      this.loading = true;
      this.events.getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.dataList = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },

    rowDel(row) {
      this.$confirm('确定将选择数据删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.events.remove(row.id);
      }).then(() => {
        this.onLoad(this.page);
        this.$message.success('删除成功');
      });
    },

    rowSave(row, done, loading) {
      this.events.add(row).then(() => {
        this.onLoad(this.page);
        this.$message.success( "操作成功!");
        done();
      }, error => {
        window.console.log(error);
        loading();
      });
    },

    rowUpdate(row, index, done, loading) {
      this.events.update(row).then(() => {
        this.onLoad(this.page);
        this.$message.success( "操作成功!");
        done();
      }, error => {
        window.console.log(error);
        loading();
      });
    },


    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return this.events.remove(this.ids);
      }).then(() => {
        this.onLoad(this.page);
        this.$message.success( "操作成功!");
        this.$refs.crud.toggleSelection();
      });
    },

    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        this.events.getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },


    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },


    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },


    selectionChange(list) {
      this.selectionList = list;
    },


    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },


    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },


    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },


    refreshChange() {
      this.onLoad(this.page, this.query);
    },


  }
}
